package com.content.view.Component.Toggle
{
	import com.library.core.IDisposable;
	import com.library.core.UIObject;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	
	import gs.TweenLite;
	
	public class ToggleItemBase extends UIObject implements IDisposable, IToggleItem
	{
		public var id:String;
		
		private var _obj:DisplayObject;
		private var _objActive:DisplayObject;
		private var _isActive:Boolean = false;
		
		public function ToggleItemBase(obj:DisplayObject, objActive:DisplayObject)
		{
			super();
			
			this.mouseChildren = false;
			this.buttonMode = true;
			
			_obj = obj;
			_objActive = objActive;			
			addChild(_obj);
			addChild(_objActive);
			
			_objActive.alpha = 0;
			_objActive.visible = false;
			
			this.addEventListener(MouseEvent.MOUSE_OVER, onOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onOut);
		}
		
		private function SwitchState(isHighlight:Boolean, unActiveDuration:Number = 0.1):void{
			if(isHighlight){
				TweenLite.to( _obj, 0.5, { autoAlpha:0, delay: 0.1 } );
				TweenLite.to( _objActive, 0.5, { autoAlpha:1 } );
			}else{
				TweenLite.to( _obj, unActiveDuration, { autoAlpha:1 } );
				TweenLite.to( _objActive, unActiveDuration, { autoAlpha:0, delay: 0.0 } );
			}
		}
		
		/* Event Handler
		* -------------------------------------------------------- */
		private function onOver(evt:MouseEvent):void{
			if(_isActive) return;
			SwitchState(true);
		}
		private function onOut(evt:MouseEvent):void{
			if(_isActive) return;
			SwitchState(false, 0.5);
		}
		
		/* Interface Implementation
		* -------------------------------------------------------- */
		public function set Active(value:Boolean):void{
			if(_isActive == value)return;
			_isActive = value;
			SwitchState(_isActive);
		}
		
		public function get Active():Boolean{	return _isActive;}
		
		public function Dispose():void
		{
			this.removeEventListener(MouseEvent.MOUSE_OVER, onOver);
			this.removeEventListener(MouseEvent.MOUSE_OUT, onOut);
		}
	}
}